<?php
/**
 * User: aka
 * Time: 2:39
 */

namespace app\read\controller;

use app\validate\mod\QueryMod;
use think\facade\Session;
use think\Request;

class Mod
{
    private $userId;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
    }

    public function __destruct()
    {
        unset($this->userId);
    }

    public function queryMod(Request $request)
    {
        $validate = new QueryMod();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryStuffName = $request->param('query_stuff_name');
            $modList = \app\tenno\model\Mod::deleteState(0)
                ->stuffName($queryStuffName)
                ->paginate(10)
                ->each(
                    function($item)
                    {
                        $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                            ->find();
                    }
                );
            return view('query_mod', ['mod_list' => $modList, 'query_name' => $queryStuffName]);
        }
    }

    public function queryUserMod(Request $request)
    {
        $validate = new QueryMod();
        if (!$validate->check($request->param())) {
            return json($validate->getError());
        }else {
            $queryStuffName = $request->param('query_stuff_name');
            $modList = \app\tenno\model\Mod::deleteState(0)
                ->stuffName($queryStuffName)
                ->uid($this->userId)
                ->paginate(10);
            return view('query_user_mod', ['list' => $modList, 'query_name' => $queryStuffName]);
        }
    }
}